#!/usr/bin/env node

require('dotenv').config()

const version = require('../package.json').version
const execute = require('child_process').execSync
const commitSha = (process.env.NOW_GITHUB_COMMIT_SHA || '').slice(0, 7)

// First two arguments are node and script name
const buildDir = process.argv[2] || 'public'

execute(`rimraf ${buildDir}`, { stdio: 'inherit' })
execute(`copy-aragon-ui-assets -n aragon-ui ./${buildDir}`, {
  stdio: 'inherit',
})

// Commit sha is provided in nightly builds
process.env.ARAGON_PACKAGE_VERSION = commitSha
  ? `${version}-${commitSha}`
  : version
execute(
  `parcel build src/index.html --out-dir ./${buildDir} --public-url ./ --no-cache`,
  { stdio: 'inherit' }
)
